/**
 * Created by yangfan on 2016/10/12.
 */
define(['config/appregister',
    "i18n",
    "business/myResume/services/httpServices",
    "business/services/constant",
    "business/services/utils",
    "business/services/filter",
    "business/services/dateComp",
    "business/services/evaluateComp",
    "business/services/regionService",
    "business/services/components"],function(app,i18n,httpServices,constant,utils){
    app.controller('resumeSearchJobCtrl', function($scope,$state,$q) {
        $scope.i18nTotal = i18n;
        $scope.i18n = i18n.resume;
        $scope.isResumeSearchPage = true;
        $scope.isResumeDetailPage = false;
        $scope.isResumeSearchListPage = false;

        var selectStyle = {"background-color":"#fff", "width":"150px"};
        var selectedStyle = {color:'#fff',background:'blue'}
        var data = [
            {
                "languageId" : 1,
                "name" : "中文"
            },
            {"languageId" : 2, "name" : "英语"}
        ];

        $scope.select = {
            position:{
              model:[
                  {
                      title:"职位",
                      type:0
                  },{
                      title:"公司",
                      type:1
                  }
              ],
                value:{
                    title:"职位",
                    type:0
                },
                key:"title",
                style:{"background-color":"#F5F5F3", "width":"74px"}
          },
            //公司性质等六个选择框
            companyNature :{
                //model:["民企","国企","外企"],
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"dictName",
                style:selectStyle
            },
            workYear :{
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"dictName",
                style:selectStyle
            },
            payScope :{
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"srName",
                style:selectStyle
            },
            companyScale :{
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"dictName",
                style:selectStyle
            },
            educational :{
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"dictName",
                style:selectStyle
            },
            workingStyle :{
                model:"",
                value:$scope.i18n.placeholderSelect,
                key:"dictName",
                style:selectStyle
            },
            releaseTime:"",
            keyWord:"",

        };
        //获取下拉表信息
        function getCompanyNature(){
            var req = {
                uri:"getCompanyNature",
                type:"GET",
            };
            httpServices.getCvdatabases(req).success(function(response){
             $scope.$apply(function(){
                 $scope.select.companyNature.model = response.data;
             })
            }).error(function(response){
                var a = response;
            })
        }
        function  workyear(){
            var req = {
                uri:"getJobExperience",
                type:"GET",
            };
            httpServices.jobServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.select.workYear.model = response.data;
                })
            }).error(function(response){
                var a = response;
            })
        }
        function  paymentZone(){
            var req = {
                uri:"getSalaryCount",
                type:"GET",
            };
            httpServices.getCvdatabases(req).success(function(response){
                $scope.$apply(function(){
                    $scope.select.payScope.model = response.data;
                })
            }).error(function(response){
                var a = response;
            })
        }
        function  getCompanyScale(){
            var req = {
                uri:"getCompanyScale",
                type:"GET",
            };
            httpServices.getCvdatabases(req).success(function(response){
                $scope.$apply(function(){
                    $scope.select.companyScale.model = response.data;
                })
            }).error(function(response){
                var a = response;
            })
        }
        function  getJobEducation(){
            var req = {
                uri:"getJobEducation",
                type:"GET",
            };
            httpServices.jobServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.select.educational.model = response.data;
                })
            }).error(function(response){
                var a = response;
            })
        }
        function  getJobNature(){
            var req = {
                uri:"getJobNature",
                type:"GET",
            };
            httpServices.jobServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.select.workingStyle.model = response.data;
                })
            }).error(function(response){
                var a = response;
            })
        }
        function getKeyWord(){
            var req = {
                type:"GET",
                uri:"getKeyWord",
                data:{
                    count:6
                }
            };
            httpServices.jobServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.keyWord = response.data;
                })
            })
        }

        getCompanyNature();
        workyear();
        paymentZone();
        getCompanyScale();
        getJobEducation();
        getJobNature();
        getKeyWord() ;


        $scope.search = function(){
            var promise =  getCurrentSelected().then(function(){
                $scope.isResumeSearchPage = false;
                $scope.isResumeDetailPage = true;
                $scope.isResumeSearchListPage = true;
                getJobList();
                listWatch();
            });
                //$scope.selected = {
                //    payScope:$scope.select.payScope.value,
                //    companyNature:$scope.select.companyNature.value,
                //    workYear:$scope.select.workYear.value,
                //    educational:$scope.select.educational.value,
                //    companyScale:$scope.select.companyScale.value,
                //    releaseTime:"",
                //    workingStyle:$scope.select.workingStyle.value,
                //};
            //var a = $scope.selected;

        };
        $scope.selected = {
            payScope:"",
            companyNature:"",
            workYear:"",
            educational:"",
            companyScale:"",
            releaseTime:"",
            workingStyle:"",
        };

        $scope.isShow = {
            calandar:false
        };
        $scope.clickItem = function(key,item){
            $scope.selected[key] = item;
            $scope.select[key].value = item;
            var promise = reSetParams(item,1).then(function(){
                getJobList();
            })

        };
        $scope.removeSelected = function(item){
            var promise = reSetParams(item,0).then(function(){
                if(item.type == "jobInJobConf"|| item.type == "jobInIndustryConf" || item.type == "workType"|| item.type == "jobInRegionIntension" || item.type == "keyWord"|| item.type == "releaseTime"){
                    // 这几类 有监听 会执行一次加载列表
                    return
                }else {
                    getJobList();
                }
              //  utils.removeArray($scope.selected,item);
            })
        };
        $scope.changeCalandar = function(){
            $scope.isShow.calandar = ! $scope.isShow.calandar;
        };
        $scope.ngMouseleaveCalandar = function(){
            $scope.isShow.startTime = false;
            $scope.isShow.endTime = false;
        };
        $scope.isSaveSearchOptions = false;

        //setType 0 表示移除  1 表示增加
        function reSetParams(item,setType){
            var defer = $q.defer();
            if(!item.hasOwnProperty("type")||(setType!=1 && setType!=0)){
                 defer.reject();
            }else{
                //薪资
                if(item.type == "paymentZone"){
                    if(setType == 1 ){
                        $scope.selected.payScope = item   ;
                    }else{
                        $scope.selected.payScope = ""   ;
                    }
                }
                //工作经验
                if(item.type == "workyear"){
                    if(setType == 1){
                        $scope.selected.workYear = item;
                    }
                        else{
                            $scope.selected.workYear = "";
                        }
                    }
                //公司规模
                if(item.type == "companyType"){
                    if(setType ==1){
                        $scope.selected.companyScale = item;
                    }else{
                        $scope.selected.companyScale = "";
                    }
                }
                //教育背景
                if(item.type == "educationBackground"){
                    if( setType == 1){
                        $scope.selected.educational = item;
                    }else{
                        $scope.selected.educational = "";
                    }
                }
                //公司性质
                if(item.type == "jobType"){
                    if(setType == 1){
                        $scope.selected.companyNature = item;
                    }else{
                        $scope.selected.companyNature = "";
                    }
                }
                //if(item.type == "workType"){
                //    return
                //}
                //
                //if(item.type == "functionsDetailType"){}
                //if(item.type == "companyGuild"){}

                //"jobInJobConf"  职能
                if(item.type == "jobInJobConf"){
                    if(setType == 1){
                        $scope.selected.jobInJobConf = item ;
                    }else{
                        $scope.selected.jobInJobConf = "";
                        $scope.jobInJobConf.detail = [];
                        $scope.jobInJobConf.showValue = "";
                    }
                }

                //"jobInIndustryConf"  行业
                if(item.type == "jobInIndustryConf"){
                    if(setType == 1){
                        $scope.selected.jobInIndustryConf = item ;
                    }else{
                        $scope.selected.jobInIndustryConf = "";
                        $scope.jobInIndustryConf.reValue = [];
                        $scope.jobInIndustryConf.showValue = "";
                    }
                }
                //"jobInIndustryConf"  工作性质
                 $scope.placeholderWorkStyle = '请选择工作性质'
                if(item.type == "workType"){
                    if(setType == 1){
                        $scope.selected.workingStyle = item ;
                    }else{
                        $scope.selected.workingStyle = "";
                        $scope.select.workingStyle.value = $scope.placeholderWorkStyle;

                    }
                }
                //"jobInIndustryConf"  期望工作地
                if(item.type == "jobInRegionIntension"){
                    if(setType == 1){
                        $scope.selected.jobInRegionIntension = item ;
                    }else{
                        $scope.selected.jobInRegionIntension = "";
                        $scope.jobInRegionIntension.reValue = [];
                        $scope.jobInRegionIntension.showValue = "";
                    }
                }
                // 关键词
                if(item.type == "keyWord"){
                    if(setType == 1){
                        $scope.selected.keyWord.title = item ;
                    }else{
                        $scope.selected.keyWord.title = "";
                    }
                }
                // 关键词
                if(item.type == "releaseTime"){
                    if(setType == 1){
                        $scope.selected.releaseTime.title = item ;
                    }else{
                        $scope.selected.releaseTime.endTime = "";
                        $scope.selected.releaseTime.startTime = "";
                        $scope.selected.releaseTime.title = "" ;
                    }
                }
                defer.resolve();
            }
            return defer.promise
        };

        //引用组件
        //期望工作地
        $scope.jobInRegionIntension = {
            reValue:[],
            isShow:false,
            showValue:"选择城市",
            maxSelect:1,
        };

        //职能
        $scope.jobInJobConf = {
            top:"",
            detail:"",
            showValue:"",
            isShow:false,
            maxSelect:1
        };

        //行业
        $scope.jobInIndustryConf = {
            detail:"",
            showValue:"",
            isShow:false,
            maxSelect:1
        };
        $scope.showSearchBox = true ;
        $scope.showWord = {
           title1:"收起过滤条件",
           title2:"展开过滤条件",
           imgUpSrc:"them/images/compImages/pull-up.png",
           imgDownSrc:"them/images/compImages/open-down.png"
        };
        $scope.hideSearchBox = function(){
            $scope.showSearchBox =!$scope.showSearchBox ;
        };
        function getCurrentSelected(){
            var deffer = $q.defer();
            $scope.selected = {
                payScope:$scope.select.payScope.value == $scope.i18n.placeholderSelect?"":$scope.select.payScope.value,
                companyNature:$scope.select.companyNature.value == $scope.i18n.placeholderSelect?"":$scope.select.companyNature.value ,
                workYear:$scope.select.workYear.value == $scope.i18n.placeholderSelect?"":$scope.select.workYear.value ,
                educational:$scope.select.educational.value == $scope.i18n.placeholderSelect?"":$scope.select.educational.value,
                companyScale:$scope.select.companyScale.value ==$scope.i18n.placeholderSelect?"":$scope.select.companyScale.value,
                workingStyle:$scope.select.workingStyle.value == $scope.placeholderWorkStyle?"":$scope.select.workingStyle.value,
                //行业 期望居住地 职能 为单选  这里选择数组 第0项
                jobInRegionIntension:$scope.jobInRegionIntension.reValue == []?"":$scope.jobInRegionIntension.reValue[0],
                type:{
                    value:$scope.select.position.value,
                    type:"type"
                },
                //多选数组
                jobInJobConf:{
                    value:$scope.jobInJobConf.detail == []?"":$scope.jobInJobConf.detail,
                    type:"jobInJobConf"
                },
                jobInIndustryConf:$scope.jobInIndustryConf.detail == []?"":$scope.jobInIndustryConf.detail[0],
                keyWord:{
                    type:"keyWord",
                    title:$scope.select.keyWord
                },

                releaseTime:{
                    startTime:"",
                    endTime:"",
                    type:"releaseTime"
                }
            };
            //地点单选
            $scope.jobInRegionIntension.type = "jobInRegionIntension";
            deffer.resolve();
            return deffer.promise
        }


        $scope.showComponent = function(key){
            $scope[key].isShow = !$scope[key].isShow ;
        };
        $scope.isShow = {
            endTime:false,
            startTime:false
        };
        $scope.changeShow = function(key){
            $scope.isShow[key] = !$scope.isShow[key];
        };
        $scope.mouseLeaveCalandar = function(){
          $scope.isShow.startTime = false;
          $scope.isShow.endTime = false;
        };


    /*   职位列表 方法*/
        $scope.listParams = {
            pageNo:1,
            pageSize:10,
            keyWord:"",
            keyWordId:"",
            type:"",

            pronviceId:"",
            regionId:"",
            areaId:"",
            functionIds:"",
            guilId:"",

            salaryId:"",
            companyNatureId:"",
            experienceId:"",
            eduId:'',
            companyScaleId:"",
            jobNatureId:"",

            startTime:"",
            endTime:"",

            overseaExp:"",
            fullTimeFlag:"",
            overseaStudy:"",
            publishTime:false,
        };

        $scope.tableTitle = ["职位","公司名称","工作地点","薪资","发布日期"];
        function getJobList(){
            var fc = utils.functionIds($scope.selected.jobInJobConf.value);
            var req = {
                type:"GET",
                uri:"getJobList",
                //data:$scope.listParams,
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    keyWord:$scope.selected.keyWord.title,
                    type:$scope.selected.type.value.type,

                    functionIds:fc.id,

                    guilId:typeof($scope.selected.jobInIndustryConf)!="undefined"&&$scope.selected.jobInIndustryConf == ""?"":$scope.selected.jobInIndustryConf.dictId,

                    salaryId: typeof($scope.selected.payScope)!="undefined"&&$scope.selected.payScope == ""?"":$scope.selected.payScope.srId,
                    companyNatureId:typeof($scope.selected.companyNature)!="undefined"&&$scope.selected.companyNature == ""?"":$scope.selected.companyNature.dictId,
                    experienceId:typeof($scope.selected.workYear)!="undefined"&&$scope.selected.workYear == ""?"":$scope.selected.workYear.dictId,
                    eduId:typeof($scope.selected.educational)!="undefined"&&$scope.selected.educational == ""?"":$scope.selected.educational.dictId,
                    companyScaleId:typeof($scope.selected.companyNature)!="undefined"&&$scope.selected.companyNature == ""?"":$scope.selected.companyNature.dictId,
                    jobNatureId:typeof($scope.selected.workingStyle)!="undefined" && $scope.selected.workingStyle == ""?"":$scope.selected.workingStyle.dictId,

                    startTime:$scope.selected.startTime,
                    endTime:$scope.selected.endTime,

                    overseaExp:"",
                    fullTimeFlag:"",
                    overseaStudy:"",
                    publishTime:$scope.listParams.publishTime,
                }
            };
            if(typeof ($scope.selected.jobInRegionIntension)!= "undefined"){
                    req.data.pronviceId = $scope.selected.jobInRegionIntension == []?"":$scope.selected.jobInRegionIntension.provinceId;
                    req.data.regionId = $scope.selected.jobInRegionIntension == []?"":$scope.selected.jobInRegionIntension.regionId;
                    req.data.areaId = $scope.selected.jobInRegionIntension == []?"":$scope.selected.jobInRegionIntension.areaId;
            }

            httpServices.jobServices(req).success(function(response){
                $scope.$apply(function(){
                  $scope.jobList = response.data.list;
                  $scope.totalItem = response.data.Pagination.totalCount;
                    if($scope.listParams.pageNo == 1){
                        getPage();
                    }
                });
            }).error(function(){})
        }
        function getPage(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getJobList();
                }
            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }


        function watchJobRegion (){
            $scope.$watch("jobInRegionIntension.showValue",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    $scope.selected.jobInRegionIntension =( $scope.jobInRegionIntension.reValue[0] === undefined?'':$scope.jobInRegionIntension.reValue[0]);
                    $scope.jobInRegionIntension.showValue = ( $scope.jobInRegionIntension.showValue == ''?'请选择':newValue)
                    if(!$scope.isResumeSearchPage){
                        getJobList();
                    }
                }
            })
        }
        watchJobRegion ();

        function listWatch(){
            if($scope.isResumeSearchPage){
                return
            }
            //分开监听 并执行相关操作
            //$scope.$watch("jobInRegionIntension.showValue",function(newValue,oldValue){
            //    if(typeof newValue !="undefined" && newValue != oldValue){
            //        $scope.selected.jobInRegionIntension =( $scope.jobInRegionIntension.reValue == []?"":$scope.jobInRegionIntension.reValue[0]);
            //        getJobList();
            //    }
            //});
            $scope.$watch("jobInJobConf.showValue",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    $scope.selected. jobInJobConf = {
                        value:$scope.jobInJobConf.detail == []?"":$scope.jobInJobConf.detail,
                            type:"jobInJobConf"
                    };
                    getJobList();
                }
            });
            $scope.$watch("jobInIndustryConf.showValue",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    $scope.selected.jobInIndustryConf =($scope.jobInIndustryConf.detail == []?"":$scope.jobInIndustryConf.detail[0]);
                    getJobList();
                }
            });
            $scope.$watch("selected.keyWord.title",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    $scope.select.keyWord = newValue ;
                    getJobList();
                }
            });
            $scope.$watch("select.workingStyle.value",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    //getList
                    $scope.selected.workingStyle = ($scope.select.workingStyle.value == $scope.placeholderWorkStyle?"":$scope.select.workingStyle.value);
                    getJobList();
                }
            });
            $scope.$watch("select.releaseTime",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                   //getList
                    getJobList();
                }
            },true);
            $scope.$watch("select.position.value.title",function(newValue,oldValue){
                if(typeof newValue !="undefined" && newValue != oldValue){
                    $scope.selected.type = {
                        value:$scope.select.position.value,
                            type:"type"
                    };
                    //getList
                    getJobList();
                }
            });
        }
        $scope.changeItemsPerPage = function(item ,index){
            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            getJobList();

        }
        $scope.changeKeyWord = function(item){
            $scope.select.keyWord  = item.name ;
        }
        $scope.picSrc = {
            sort:'them/images/compImages/icon-sort.png',
            drop:'them/images/compImages/icon-drop.png',
        };

        //根据时间排序
        $scope.sortByTime = function () {
            $scope.listParams.publishTime = !$scope.listParams.publishTime;
            getJobList();
        };


    })
});
